package org.stvd.controller.workflow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Users;
import org.stvd.entities.workflow.WfGroup;
import org.stvd.service.admin.UsersService;
import org.stvd.service.workflow.WfGroupService;

/**
 * @Title WorkflowGroupController
 * @Destribution 流程模型选分组控制类
 * @author wwh
 * @date 2020年10月27日
 */
@Controller
@RequestMapping("/workflow/group")
public class WfGroupController extends BaseController {
	@Autowired
	private WfGroupService wfGroupService;
	@Autowired
	private UsersService usersService;

	private String tmURL = "/workflow/group/";

	@RequestMapping(value = "select/{ids}")
	public String groupchoosemanage(@PathVariable String ids) {
		List<Users> userList = usersService.findAll(Users.class);
		map.put("userList", userList);
		QueryResult<WfGroup> queryResult = wfGroupService.wfGroupQuery((pageNo - 1) * pageSize, pageSize, "", "", "");
		map.put("queryResult", queryResult);
		map.put("ids", ids);
		map.put("groupList", queryResult.getRows());
		map.put("totalPageNum", queryResult.getPageCount());
		map.put("currentPage", queryResult.getPageIndex());
		map.put("pageSize", pageSize);
		return tmURL + "GroupList.html";
	}

	@RequestMapping(value = "names")
	@ResponseBody
	public Map<String, Object> getUnames() {
		Map<String, Object> data = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String unames = "";
		if (ids != null && ids.length() > 0) {
			String[] uidList = ids.split(",");
			for (int i = 0; i < uidList.length; i++) {
				WfGroup group = wfGroupService.getWfGroupByCode(uidList[i]);
				if(group!=null) {
					unames = unames.concat(group.getName() + ",");
				}
			}
			if(unames.endsWith(",")) {
				unames = unames.substring(0, unames.length() - 1);
			}
		}
		data.put("name", unames);
		return data;
	}

	@RequestMapping(value = "loadData")
	@ResponseBody
	public Map<String, Object> loadData() {
		String pageNo = request.getParameter("pageNo");
		String name = request.getParameter("name");
		if (name == null) {
			name = "";
		}
		Integer pageNum = 1;
		if (!StringUtil.isEmpty(pageNo)) {
			pageNum = Integer.parseInt(pageNo);
		}
		Map<String, Object> data = new HashMap<String, Object>();
		QueryResult<WfGroup> queryResult = wfGroupService.wfGroupQuery((pageNum - 1) * pageSize, pageSize, name, "",
				"");
		data.put("queryResult", queryResult);
		List<WfGroup> groupList = queryResult.getRows();
		data.put("groupList", groupList);
		data.put("totalPageNum", queryResult.getPageCount());
		data.put("currentPage", queryResult.getPageIndex());
		data.put("pageSize", pageSize);
		return data;
	}
}
